package com.tencent.sr.iris.activity.dependency;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.interfaces.client.IDCClient;
import com.tencent.sr.rmall.interfaces.request.IdcRequest;
import com.tencent.sr.rmall.interfaces.response.IdcResponse;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IDCDependency {

    private static final String BASIC_BIZ_TAG = "iris-activity";

    @Resource
    private IDCClient idcClient;

    public Long genId() {
        IdcRequest request = new IdcRequest();
        request.setBizTag(BASIC_BIZ_TAG);

        HttpResult<IdcResponse> res = idcClient.getId(request);
        if (res != null && res.isSuccess() && res.getData() != null) {
            return res.getData().getId();
        }
        log.error("idcClient.getId error, req:{},res:{}", JSON.toJSONString(request), JSON.toJSONString(res));
        throw new TRetailBizException("获取分布式id失败");
    }
}